package com.example.demo.component.zibbix;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName zibbix_client_factory
 * @Description
 * @date 2022/11/10 17:38
 * @Author yanceysong
 * @Version 1.0
 */
public class zibbix_client_factory {
    private static final ConcurrentHashMap<String, zibbix_client> zibbix_client_pool = new ConcurrentHashMap<>();
    private static final String zibbix_server_ip = "172.21.1.42:9000";
    private static final String username = "Admin";
    private static final String password = "zabbix";

    /**
     * 获取zibbix客户端
     *
     * @return 客户端
     */
    public static zibbix_client get_zibbix_client() {
        if (!zibbix_client_pool.containsKey(zibbix_server_ip)) {
            zibbix_client_pool.put(zibbix_server_ip, new zibbix_client(zibbix_server_ip, username, password));
        }
        return zibbix_client_pool.get(zibbix_server_ip);
    }

    /**
     * 如果客户端失效可以利用该方法更新客户端
     *
     * @return 更新后客户端
     */
    public zibbix_client update_zibbix_client() {
        zibbix_client_pool.remove(zibbix_server_ip);
        return get_zibbix_client();
    }
}
